home *** CD-ROM | disk | FTP | other *** search
- package tetris;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import javax.microedition.lcdui.ChoiceGroup;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.Gauge;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.Item;
- import javax.microedition.lcdui.ItemStateListener;
- import javax.microedition.rms.RecordStore;
-
- public class Options extends Form implements CommandListener, ItemStateListener {
- static int keyLeft;
- static int keyRight;
- static int keyUp;
- static int keyDown;
- static boolean optSounds = false;
- static boolean optPreview = true;
- static boolean optDefaultKeys = true;
- static boolean optProgressive = true;
- static int startLevel = 0;
- static int garbageLevel = 0;
- static RecordStore optionsRS;
- static ChoiceGroup opt1;
- static ChoiceGroup opt2;
- static Gauge opt3;
- static Gauge opt4;
-
- public Options() {
- super("Options");
-
- try {
- this.jbInit();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- protected static void openStore() {
- try {
- optionsRS = RecordStore.openRecordStore("options", true);
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- protected static void closeStore() {
- try {
- optionsRS.closeRecordStore();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- private void jbInit() throws Exception {
- ((Displayable)this).setCommandListener(this);
- ((Form)this).setItemStateListener(this);
- ((Displayable)this).addCommand(new Command("OK", 2, 1));
- openStore();
- readOptions();
- closeStore();
- opt1 = new ChoiceGroup("", 2, new String[]{"Preview", "Progressive"}, (Image[])null);
- ((Form)this).append(opt1);
- opt2 = new ChoiceGroup("", 2, new String[]{"Default keys"}, (Image[])null);
- ((Form)this).append(opt2);
- opt3 = new Gauge("Starting level", true, 4, startLevel);
- ((Form)this).append(opt3);
- opt4 = new Gauge("Garbage level", true, 4, garbageLevel);
- ((Form)this).append(opt4);
- opt1.setSelectedFlags(new boolean[]{optPreview, optProgressive});
- opt2.setSelectedFlags(new boolean[]{optDefaultKeys});
- }
-
- public void commandAction(Command command, Displayable displayable) {
- if (command.getCommandType() == 3) {
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(this);
- } else if (command.getCommandType() == 4) {
- TetrisMIDlet.mainMenu.redefineKeys(this);
- } else if (command.getCommandType() == 2) {
- optPreview = opt1.isSelected(0);
- optDefaultKeys = opt2.isSelected(0);
- optProgressive = opt1.isSelected(1);
- startLevel = opt3.getValue();
- garbageLevel = opt4.getValue();
- openStore();
- saveOptions();
- closeStore();
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
- }
-
- }
-
- public void itemStateChanged(Item item) {
- if (item == opt2 && !opt2.isSelected(0)) {
- Form ck = new Form("Current keys");
- optDefaultKeys = false;
- ck.append(currentKeys(false));
- ((Displayable)ck).addCommand(new Command("Redefine", 4, 1));
- ((Displayable)ck).addCommand(new Command("OK", 3, 2));
- ((Displayable)ck).setCommandListener(this);
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(ck);
- }
-
- }
-
- public static void saveOptions() {
- try {
- byte[] b = new byte[]{(byte)(optSounds ? 1 : 0), (byte)(optPreview ? 1 : 0), (byte)(optDefaultKeys ? 1 : 0), (byte)(optProgressive ? 1 : 0), (byte)startLevel, (byte)garbageLevel};
- if (optionsRS.getNumRecords() > 0) {
- optionsRS.setRecord(1, b, 0, b.length);
- } else {
- optionsRS.addRecord(b, 0, b.length);
- }
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- public static void saveKeys() {
- openStore();
-
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream dos = new DataOutputStream(baos);
- dos.writeInt(keyLeft);
- dos.writeInt(keyRight);
- dos.writeInt(keyUp);
- dos.writeInt(keyDown);
- byte[] b = baos.toByteArray();
- if (optionsRS.getNumRecords() > 1) {
- optionsRS.setRecord(2, b, 0, b.length);
- } else {
- saveOptions();
- optionsRS.addRecord(b, 0, b.length);
- }
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- closeStore();
- }
-
- public static void readOptions() {
- try {
- if (optionsRS.getNumRecords() > 0) {
- byte[] b = optionsRS.getRecord(1);
- optSounds = b[0] == 1;
- optPreview = b[1] == 1;
- optDefaultKeys = b[2] == 1;
- optProgressive = b[3] == 1;
- startLevel = b[4];
- garbageLevel = b[5];
- }
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- public static boolean readKeys() {
- openStore();
-
- try {
- if (optionsRS.getNumRecords() > 1) {
- byte[] b = optionsRS.getRecord(2);
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream dis = new DataInputStream(bais);
- keyLeft = dis.readInt();
- keyRight = dis.readInt();
- keyUp = dis.readInt();
- keyDown = dis.readInt();
- closeStore();
- boolean var3 = true;
- return var3;
- }
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- closeStore();
- return false;
- }
-
- static String currentKeys(boolean b) {
- String k1;
- String k2;
- String k3;
- String k4;
- if (optDefaultKeys) {
- k1 = TetrisMIDlet.gameScreen.getKeyName(TetrisMIDlet.gameScreen.getKeyCode(1));
- k2 = TetrisMIDlet.gameScreen.getKeyName(TetrisMIDlet.gameScreen.getKeyCode(6));
- k3 = TetrisMIDlet.gameScreen.getKeyName(53);
- k4 = TetrisMIDlet.gameScreen.getKeyName(TetrisMIDlet.gameScreen.getKeyCode(2));
- } else {
- k1 = TetrisMIDlet.gameScreen.getKeyName(keyLeft);
- k2 = TetrisMIDlet.gameScreen.getKeyName(keyRight);
- k3 = TetrisMIDlet.gameScreen.getKeyName(keyUp);
- k4 = TetrisMIDlet.gameScreen.getKeyName(keyDown);
- }
-
- return new String(String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf(b ? "Current keys:\n" : "")))).append(k1).append(" - left\n").append(k2).append(" - right\n").append(k3).append(" - rotate\n").append(k4).append(" - drop"))));
- }
- }
-